Converting Between Geometric Shape Types
QuickDraw GX provides theGXGetShapeType
andGXSetShapeType
functions to allow you to manipulate a shape's type. TheGXGetShapeType
function simply returns the value of the shape type property for a specified shape. For geometric shapes, the possible values returned from this function are
The
gxEmptyType
gxFullType
gxPointType
gxLineType
gxCurveType
gxRectangleType
gxPolygonType
gxPathType
GXSetShapeType
function allows you to change the shape type of an existing shape. In doing so, this function often has to reinterpret the geometry of the shape. This reinterpretation is called type conversion. Sometimes the conversion makes sense and doesn't lose any data. For example, you might want to convert a line shape to a polygon shape so that you can add more contours to the shape. Some conversions, however, aren't as useful and data can be lost. For example, converting a complex path shape to a point shape can result in the loss of a significant amount of data.In general, when converting between geometric shape types, QuickDraw GX exhibits different behavior in these four situations:
When converting to an empty shape or a full shape, all the information in the original shape's geometry is lost--the result is simple an empty shape or a full shape, respectively.
- when converting other geometric shapes to an empty shape or a full shape
- when converting other geometric shapes to a point, line, or rectangle
- when converting other geometric shapes to a curve
- when converting other geometric shapes to a polygon or path
The following three subsections discuss the other cases in more detail.
Converting Shapes to Points, Lines, and Rectangles
When converting a shape to a point, line, or rectangle, QuickDraw GX uses the bounding rectangle of the original shape. (Bounding rectangles are defined in the chapter "Geometric Operations" in this book. For an example, see Figure 2-41 on page 2-68.) QuickDraw GX uses the bounding rectangle differently, depending on which shape type you are converting to:
Listing 2-19 creates a path shape, which is converted subsequently to a rectangle shape, then to a line shape, and finally to a point shape.
- When you convert to a rectangle shape, the resulting rectangle is the bounding rectangle of the original shape.
- When you convert to a line shape, the result is a line that runs from the upper-left corner of the original shape's bounding rectangle to the lower-right corner.
- When you convert to a point, the resulting point is the point at the upper-left corner of the bounding rectangle of the original shape.
Listing 2-19 Creating a figure-eight path shape
void CreateFigureEight(void) { gxShape aPathShape; static long figureEightGeometry[] = {1,/* number of contours */ 4, /* number of points */ 0xF0000000, /* 1111 ... */ ff(50), ff(50), /* off */ ff(200),ff(200), /* off */ ff(50), ff(200), /* off */ ff(200),ff(50)}; /* off */ aPathShape = GXNewPaths((gxPaths *) figureEightGeometry); GXSetShapeFill(aPathShape, gxClosedFrameFill); GXDrawShape(aPathShape); }The resulting path geometry is shown in Figure 2-40.Figure 2-40 A figure-eight path shape
If you convert this shape to a rectangle shape, using the call
GXSetShapeType(aPathShape, gxRectangleType);the resulting shape is the bounding rectangle for the original path shape, as shown in Figure 2-41.Figure 2-41 A path shape before and after conversion to a rectangle shape
If you convert the original path shape to a line shape, using the call
GXSetShapeType(aPathShape, gxLineType);the resulting shape is a diagonal line from the upper-left corner of the path's bounding rectangle to its lower-right corner, as shown in Figure 2-42.Figure 2-42 A path shape before and after conversion to a line shape
Finally, if you convert the orignal path shape to a point shape, using the call
GXSetShapeType(aPathShape, gxPointType);the resulting shape is the point at the upper-left corner of the path's bounding rectangle, as shown in Figure 2-43.Figure 2-43 A path shape before and after conversion to a point shape
The next two sections give examples of converting to the curve shape type and of converting to the polygon and path shape types.
For more information about the
GXSetShapeType
function, see the chapter "Shape Objects" of Inside Macintosh: QuickDraw GX Objects.Converting Shapes to Curve Shapes
When converting other geometric shapes to a curve shape, QuickDraw GX takes one of these approaches:
The sample function in Listing 2-20 creates a line shape and converts it to a curve.
- When converting a point to a curve, QuickDraw GX sets each of the three geometric points of the curve to be the same as the original point.
- When converting a line to a curve, QuickDraw GX sets the first point and last point of the curve to be the same as the first point and last point of the line and sets the off-curve control point to be same as the last point of the line, which results in the curve being a straight line.
- When converting a rectangle to a curve, QuickDraw GX sets the first point of the curve to be the upper-left corner of the rectangle and the last point of the curve to be the lower-right corner of the rectangle. The off-curve control point is set to be the same as the last point, which results in the curve being a straight line.
- When converting a polygon or a path to a curve, QuickDraw GX sets the three geometric points of the curve to be the first three geometric points of the original shape.
Listing 2-20 Converting a line to a curve
void ConvertLineToCurve(void) { gxShape aLineShape; static gxLine diagonalGeometry = {ff(50), ff(50), ff(150), ff(150)}; aLineShape = GXNewLine(&diagonalGeometry); GXSetShapeType(aLineShape, gxCurveType); GXDrawShape(aLineShape); GXDisposeShape(aLineShape); }The original line shape and the resulting curve shape are shown in Figure 2-44.Figure 2-44 A line shape before and after conversion to a curve shape
Notice that the converted curve looks just like the original line. The only difference between the two shapes is that the curve shape has an additional off-curve control point, which is set to be identical to the last point.
The sample function in Listing 2-21 creates a rectangle shape and converts it to a curve shape.
Listing 2-21 Converting a rectangle to a curve
void ConvertRectangleToCurve(void) { gxShape aRectangleShape; static gxRectangle rectangleGeometry = {ff(50), ff(50), ff(150), ff(150)}; aRectangleShape = GXNewRectangle(&rectangleGeometry); GXSetShapeType(aRectangleShape, gxCurveType); GXDrawShape(aRectangleShape); GXDisposeShape(aLineShape); }The original rectangle and the resulting curve are both shown in Figure 2-45.Figure 2-45 A rectangle shape before and after conversion to a curve shape
As in the previous example, the off-curve control point of the curve shape is set to be the same as the last point, which results in the curve shape being a straight line.
The next example, shown in Listing 2-22, shows how QuickDraw GX converts a polygon shape to a curve shape.
Listing 2-22 Converting a polygon shape to a curve shape
void ConvertPolygonToCurve(void) { gxShape aPolygonShape; static long aPolygonGeometry[] = {1, /* number of contours */ 4, /* number of points */ ff(50), ff(50), ff(150), ff(50), ff(150), ff(150), ff(50), ff(150)}; aPolygonShape = GXNewPolygons((gxPolygons *) aPolygonGeometry); GXSetShapeType(aPolygonShape, gxCurveType); GXDrawShape(aPolygonShape); GXDisposeShape(aPolygonShape); }In this example, QuickDraw GX sets the three geometric points of the resulting curve to be the first three geometric points of the original polygon. (Converting from path shapes to curve shapes works in the same way.) The original polygon and the resulting curve are shown in Figure 2-46.Figure 2-46 A polygon shape before and after conversion to a curve shape
Notice that even though the polygon in this example looks the same as the rectangle in Figure 2-45, the converted curve shape looks quite different.
The next section gives examples of converting shapes to polygon and path shapes.
For more information about the
GXSetShapeType
function in general, see the chapter "Shape Objects" of Inside Macintosh: QuickDraw GX Objects.Converting Shapes to Polygons and Paths
When converting other geometric shapes to polygon or path shapes, the original shapes don't lose any geometric information. For example, when you convert a line shape to a path shape, the resulting path shape contains one contour with two geometric points, both on curve--an exact duplicate of the original line.You can even convert a curve shape to a polygon shape without losing geometric information, although the result does draw differently. The resulting polygon has one contour and three geometric points--the same three geometric points as the original curve. If you convert the polygon back to a curve, you end up with the original curve.
When you convert a rectangle shape to a polygon shape, as shown in Listing 2-23, the original shape and the resulting shape look exactly the same.
Listing 2-23 Converting a rectangle shape to a polygon shape
void ConvertRectangleToPolygon(void) { gxShape aRectangleShape; static gxRectangle rectangleGeometry = {ff(50), ff(50), ff(150), ff(150)}; aRectangleShape = GXNewRectangle(&rectangleGeometry); GXSetShapeType(aRectangleShape, gxPolygonType); GXDrawShape(aRectangleShape); GXDisposeShape(aRectangleShape); }The original rectangle and the resulting polygon are shown in Figure 2-47.Figure 2-47 A rectangle shape before and after conversion to a polygon shape
Converting from a path shape to a polygon shape, however, does lose geometric information. The resulting polygon contains the same geometric points as the original path; however, the points are all considered on-curve points. The original information about which points were on curve and which points were off curve is lost during this conversion.
As an example, Listing 2-24 creates a path shape and converts it to a polygon shape.
Listing 2-24 Converting a path shape to a polygon shape
void ConvertPathToPolygon(void) { gxShape aPathShape; static long figureEightGeometry[] = {1, /* # of contours */ 4, /* # of points */ 0xF0000000, /* 1111 ... */ ff(50), ff(50), /* off */ ff(200),ff(200), /* off */ ff(50), ff(200), /* off */ ff(200),ff(50)}; /* off */ aPathShape = GXNewPaths((gxPaths *) figureEightGeometry); GXSetShapeFill(aPathShape, gxHollowFill); GXSetShapeType(aPathShape, gxPolygonType); GXDrawShape(aPathShape); GXDisposeShape(aPathShape); }Figure 2-48 shows the original path shape and the resulting polygon shape.Figure 2-48 A path shape before and after conversion to a polygon shape
Converting in the other direction, however--from a polygon shape to a path shape--retains all of the geometry information and the resulting path shape looks exactly the same as the original polygon shape. The sample function in Listing 2-25 gives an example.
- Note
- You can request that QuickDraw GX calculate a better polygon approximation to a path by setting the curve error property of the path shape's style object before calling the
GXSetShapeType
function. See the next chapter, "Geometric Styles," for examples.![]()
Listing 2-25 Converting a polygon shape to a path shape
void ConvertPolygonToPath(void) { gxShape aPolygonShape; static long aPolygonGeometry[] = {2, /* number of contours */ 3, /* number of points */ ff(50), ff(50), ff(100), ff(80), ff(50), ff(110), 3, /* number of points */ ff(200), ff(50), ff(150), ff(80), ff(200), ff(110)}; aPolygonShape = GXNewPolygons((gxPolygons *) aPolygonGeometry); GXSetShapeType(aPolygonShape, gxPathType); GXDrawShape(aPolygonShape); GXDisposeShape(aPolygonShape); }The original polygon shape and the converted path shape are shown in Figure 2-49.Figure 2-49 Polygon shape with two contours before and after conversion to a path shape
For more information about the
GXSetShapeType
function, see the chapter "Shape Objects" of Inside Macintosh: QuickDraw GX Objects.
Main | Page One | What's New | Apple Computer, Inc. | Find It | Contact Us | Help